import store from '@/store'

function hasName(menus, val, obj) {
	for (const menu of menus) {
		if (menu.name === val) {
			obj.flag = true
			break
		} else if (menu.children && Array.isArray(menu.children)) {
			hasName(menu.children, val, obj)
		}
	}
}

function checkPermission(el, binding) {
	const { value } = binding
	// console.log(store);
	const menus = store.getters['log/menuList']
	if (value) {
		let obj = { flag: false }
		hasName(menus, value, obj)
		if (!obj.flag) {
			el.parentNode && el.parentNode.removeChild(el)
		}
	} else {
		throw new Error(`need menus! Like v-permission="myFortune"`)
	}
}

export default {
	inserted(el, binding) {
		checkPermission(el, binding)
	},
	update(el, binding) {
		checkPermission(el, binding)
	}
}
